//
//  TransitionAnimation.m
//  F FloatingPlayer
//
//  Created by yonas on 2018/9/27.
//  Copyright © 2018年 Inspur. All rights reserved.
//

#import "TransitionAnimation.h"
#import "UITabBarController+FloatingTabbarExtension.h"
@interface TransitionAnimation ()
@property(nonatomic,strong) id <UIViewControllerContextTransitioning> transitionContext;
@property(nonatomic,assign) TransitionType type;
@end


@implementation TransitionAnimation

+ (instancetype)transitionWithType:(TransitionType)type{
    return [[self alloc]initWithType:type];
}

- (instancetype)initWithType:(TransitionType)type{
    if (self = [super init]){
        _type = type;
    }
    return self;
}
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext
{
    self.transitionContext = transitionContext;
    return .5f;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    if (_type == TransitionTypePush){
        [self _animationPresentWithContext:transitionContext];
    }else if (_type == TransitionTypePop){
        [self _animationDismissWithContext:transitionContext];
    }
}

#pragma mark - private
- (void)_animationDismissWithContext:(id <UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UITabBarController *tabVc = (UITabBarController *)fromVc.tabBarController;
    UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    UIView *tempView = [fromVc.view snapshotViewAfterScreenUpdates:YES];
    [containerView addSubview:tempView];
    [containerView addSubview:toVc.view];
    toVc.view.alpha = 0.1f;
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        [tabVc getFloatingWindow].frame = [tabVc getViewFrame];
        toVc.view.alpha = 1.0f;
    }completion:^(BOOL finished) {
        [self.transitionContext completeTransition:YES];
    }];
}

- (void)_animationPresentWithContext:(id <UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *realfromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UITabBarController *tabVc = realfromVc.tabBarController;
    UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    UIView *tempView = [realfromVc.view snapshotViewAfterScreenUpdates:YES];
    [containerView addSubview:tempView];
    [containerView addSubview:toVc.view];
    toVc.view.alpha = 0.1f;
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        [tabVc getFloatingWindow].frame = [tabVc getOriginalFrame];
        toVc.view.alpha = 1.0f;
    }completion:^(BOOL finished) {
        [self.transitionContext completeTransition:YES];
    }];
}

@end
